//
//  FDMBroadcastCollectionViewCell.m
//  Entertainment
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMBroadcastCollectionViewCell.h"
#import "FDMBroadcastLIstCollectionViewCell.h"
#import "FDMBroadcastPushFMCollectionViewCell.h"
#import "FDMBroadcastRecommandRadioListModel.h"
#import "FDMBroadrectTopRadioListModel.h"
#import "FDMBroadcastFMCollectionViewCell.h"
#import "FDMHotRecommendsHeaderView.h"

@interface FDMBroadcastCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) FDMBaseCollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *recommandRadioListModelArray;/**< 推荐电台 */
@property (nonatomic, retain) NSMutableArray *topRadioListModelArray;/**< 排行榜 */

@property (nonatomic, retain) NSMutableArray *FMPicArr;/**< 电台图片数组 */

@end
@implementation FDMBroadcastCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
        [self dataHandel];
        self.recommandRadioListModelArray = [NSMutableArray array];
        self.topRadioListModelArray = [NSMutableArray array];
        self.FMPicArr = [NSMutableArray arrayWithObjects:@"liveLocal@2x.png", @"liveCountry@2x.png", @"liveProvince@2x.png", @"liveNet@2x.png", nil];
    }
    return self;
}

- (void)dealloc {
    [self.collectionView release];
    [self.recommandRadioListModelArray release];
    [self.topRadioListModelArray release];
    [self.FMPicArr release];
    [super dealloc];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[FDMBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 153) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView registerClass:[FDMBroadcastFMCollectionViewCell class] forCellWithReuseIdentifier:@"FDMBroadcastFMCollectionViewCell"];
    [self.collectionView registerClass:[FDMBroadcastPushFMCollectionViewCell class] forCellWithReuseIdentifier:@"FDMBroadcastPushFMCollectionViewCell"];
    [self.collectionView registerClass:[FDMBroadcastLIstCollectionViewCell class] forCellWithReuseIdentifier:@"FDMBroadcastLIstCollectionViewCell"];
    [self.collectionView registerClass:[FDMHotRecommendsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMBroadcastHeaderView"];
    
}

#pragma mark - collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeZero;
    }
    return CGSizeMake(SCREEN_WIDTH, 30);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake((SCREEN_WIDTH - 50) / 4, 100);
    }
    if (indexPath.section == 1) {
        return CGSizeMake((SCREEN_WIDTH - 42) / 3, 150);
    } else {
        return CGSizeMake(SCREEN_WIDTH - 20, 100);
    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMBroadcastHeaderView" forIndexPath:indexPath];
            header.titleLabel.text = @"推荐电台";
            return header;
        }
    }
    if (indexPath.section == 2) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMBroadcastHeaderView" forIndexPath:indexPath];
            header.titleLabel.text = @"排行榜";
            return header;
        }
    }
    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    if (section == 1) {
        return self.recommandRadioListModelArray.count;
    }
    return self.topRadioListModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        FDMBroadcastFMCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMBroadcastFMCollectionViewCell" forIndexPath:indexPath];
        cell.titleImageView.image = [UIImage imageNamed:self.FMPicArr[indexPath.row]];
        return cell;
    }
    if (indexPath.section == 1) {
        FDMBroadcastPushFMCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMBroadcastPushFMCollectionViewCell" forIndexPath:indexPath];
        FDMBroadcastRecommandRadioListModel *model = self.recommandRadioListModelArray[indexPath.row];
        cell.recommandModel = model;
        return cell;
    } else {
        FDMBroadcastLIstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMBroadcastLIstCollectionViewCell" forIndexPath:indexPath];
        FDMBroadrectTopRadioListModel *model = self.topRadioListModelArray[indexPath.row];
        cell.topRadioModel = model;
        return cell;
    }
    
}

- (void)dataHandel {
    [APPTools GETWithURL:@"http://live.ximalaya.com/live-web/v1/getHomePageRadiosList?statEvent=pageview/tab@发现_广播" par:nil success:^(id responseObject) {
        self.recommandRadioListModelArray = [FDMBroadcastRecommandRadioListModel baseModelHandelWithArray:responseObject[@"result"][@"recommandRadioList"]];
        self.topRadioListModelArray = [FDMBroadrectTopRadioListModel baseModelHandelWithArray:responseObject[@"result"][@"topRadioList"]];
        [self.collectionView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
